package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import com.example.myapplication.db.User;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Lx on 2018/8/10.
 */

public class ListActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
    RecyclerView view;
    //记录列表
    List<User> beenList;


    SwipeRefreshLayout swipeRefreshLayout;
    mAdapter mAdapter;

    int mPage = 1;

    String Tag = "ListActivity";
    int number;
    int index;
    List<User> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        number = Utils.getInstance().getContent();
        init();
        initDate();
        Log.e(Tag, Tag);
    }

    private void init() {
        view = findViewById(R.id.rv);
        swipeRefreshLayout = findViewById(R.id.sw);
        swipeRefreshLayout.setOnRefreshListener(this);

    }

    //            mAdapter.notifyDataSetChanged();
    private void initDate() {
        number = number - 5;
        list = Utils.getInstance().getList(mPage, number);
        if (list != null) {
            beenList = list;
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
            view.setLayoutManager(linearLayoutManager);
            mAdapter = new mAdapter(R.layout.item, beenList);
            view.setAdapter(mAdapter);
        } else {
            Log.e("TAG", "数据为空");
        }

    }


    private void getRecord(final int page) {

        Observable.create(new ObservableOnSubscribe<List<User>>() {
            @Override
            public void subscribe(ObservableEmitter<List<User>> emitter) throws Exception {
                List<User> list = Utils.getInstance().getList(page, number);
                if (list != null) {
                    //插入开头
                    emitter.onNext(list);
                } else {
                    swipeRefreshLayout.setRefreshing(false);
                }

            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<List<User>>() {

                    @Override
                    public void accept(List<User> users) throws Exception {
                        beenList.addAll(0, users);
                        mAdapter.notifyItemRangeInserted(0, users.size());
                        mAdapter.notifyDataSetChanged();
                        swipeRefreshLayout.setRefreshing(false);
                    }
                });


    }

    @Override
    public void onRefresh() {

        if (Utils.isMoreRecord) {
            Log.e(Tag, "有数据");
            mPage++;
            number = number - 5;
            Log.e("listActivity", number + "");
            getRecord(mPage);
        } else {
            Toast.makeText(ListActivity.this, "没有更多的数据了", Toast.LENGTH_SHORT).show();
            swipeRefreshLayout.setRefreshing(false);
        }
    }
}